package baumdurchlaeufe;

public class BinaerBaum
{
  private Node root;
  
  public class Node
  {
    public Object value;
    public Node right;
    public Node left;

    public Node(Object value)
    {
      this.value = value;
    }
  }
  
  public BinaerBaum(Object rootValue)
  {
    root = new Node(rootValue);
  }

  public Node getRoot()
  {
    return root;
  }
  
  public int getLevelSum(int level)
  {
    return runSearch(level, 0, root);
  }
  
  public void autoFill()
  {
    root.left = new Node(2);
    root.right = new Node(3);
    
    root.left.left = new Node(4);
    root.left.right = new Node(5);
    root.right.left = new Node(6);
    root.right.right = new Node(7);
    
    root.left.left.left = new Node(8);
    root.left.left.right = new Node(9);
    root.left.right.left = new Node(10);
    root.left.right.right = new Node(11);
    root.right.left.left = new Node(12);
    root.right.left.right = new Node(13);
    root.right.right.left = new Node(14);
    root.right.right.right = new Node(15);
    
    root.right.right.left.right = new Node(16);
    root.right.right.right.left = new Node(17);
    root.right.right.right.right = new Node(18);
  }

  private int runSearch(int targetLevel, int currentLevel, Node n)
  {
    if(currentLevel == targetLevel)
      return (Integer)n.value;
    int l = 0;
    int r = 0;
    if(n.left != null)
      l = runSearch(targetLevel, currentLevel+1, n.left);
    if(n.right != null)
      r = runSearch(targetLevel, currentLevel+1, n.right);
    return l+r;
  }
  
  public String toString()
  {
    String output = "";
    return output;
  }
}

